<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 组织架构--头部-->
      <el-card class="tree-card">
        <tree-tools :tree-node="company" is-root @addDepts="addDepts" />
        <!-- tree 树型结构 -->
        <el-tree :data="departs" :props="defaultProps" :default-expand-all="true">
          <tree-tools slot-scope="{ data }" :tree-node="data" @addDepts="addDepts" @editDepts="editDepts" @delDepts="getDepartments" />
          <!-- <div slot-scope="{ data }">{{ data }}</div> -->
        </el-tree>
      </el-card>
    </div>
    <add-dept ref="addDept" :current-node="currentNode" :show-dialog.sync="showDialog" @addDepts="getDepartments" />
  </div>
</template>

<script>
import TreeTools from './components/tree-tools'
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import AddDept from './components/add-dept'
export default {
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      company: { name: '', mangager: '' },
      defaultProps: {
        label: 'name'
      },
      departs: [/* { name: '总裁办', manager: '曹操', children: [{ name: '董事会', manager: '曹丕', children: [{ name: '3' }] }] },
        { name: '行政部', manager: '刘备' },
        { name: '人事部', manager: '孙权' } */],
      showDialog: false,
      currentNode: null
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getDepartments()
  },
  mounted() {},
  methods: {
    // 获取最新组织架构数据
    async getDepartments() {
      const result = await getDepartments()
      this.company = { name: '鑫达科技产业发展有限公司' || result.companyName, manager: '负责人', id: '' }
      this.departs = tranListToTreeData(result.depts, '')
    },
    addDepts(currentNode) {
      this.showDialog = true
      this.currentNode = currentNode
    },
    editDepts(currentNode) {
      this.showDialog = true
      this.currentNode = currentNode
      this.$refs.addDept.getDepartDetail(currentNode.id)
    }
  }
}
</script>

<style scoped lang="scss">
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
